#!/usr/bin/php -d mysqlnd.net_read_timeout=3600
<?php
set_include_path("../../../include");
require("header.inc.php");

$startShard = !empty($argv[1]) ? $argv[1] : 1;
$shardIDs = Zotero_DB::columnQuery("SELECT shardID FROM shards WHERE shardID >= ? ORDER BY shardID", $startShard);

foreach ($shardIDs as $shardID) {
	echo "Shard: $shardID\n";
	$libraryIDs = Zotero_DB::columnQuery("SELECT libraryID FROM shardLibraries ORDER BY libraryID", false, $shardID);
	foreach ($libraryIDs as $libraryID) {
		echo "$libraryID ";
		$sql = "UPDATE shardLibraries SET storageUsage=("
			. "SELECT IFNULL(SUM(size), 0) FROM storageFileItems JOIN items USING (itemID) WHERE libraryID=?"
			. ") WHERE libraryID=?";
		Zotero_DB::query($sql, [$libraryID, $libraryID], $shardID);
	}
	echo "\n\n";
}
